﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace NakedSanta
{
    class Building : GameObject
    {
        private const int FTR = 0;
        private const int FTL = 1;
        private const int FBR = 2;
        private const int FBL = 3;

        private const int BTR = 4;
        private const int BTL = 5;
        private const int BBR = 6;
        private const int BBL = 7;

        private const int LTF = 8;
        private const int LTB = 9;
        private const int LBF = 10;
        private const int LBB = 11;

        private const int RTF = 12;
        private const int RTB = 13;
        private const int RBF = 14;
        private const int RBB = 15;

        
        private const int TLF = 16;
        private const int TLB = 17;
        private const int TRF = 18;
        private const int TRB = 19;

        Color color;

        int height;

        public Building(GraphicsDevice graphicsDevice, Color color, int height, Vector3 position)
        {
            this.color = color;
            this.position = position;
            this.height = height;
            this.graphicsDevice = graphicsDevice;

            InitializeVertices();
            InitializeIndices();

            vertexBuffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionColorNormal), vertices.Length, BufferUsage.WriteOnly);
            indexBuffer = new IndexBuffer(graphicsDevice, typeof(short), indices.Length, BufferUsage.WriteOnly);

            vertexBuffer.SetData<VertexPositionColorNormal>(vertices);
            indexBuffer.SetData<short>(indices);
        }

        private void InitializeVertices()
        {
            vertices = new VertexPositionColorNormal[20];
            
            //top left front
            vertices[TLF] = new VertexPositionColorNormal(new Vector3(0, height, 0), color);
            vertices[TLF].Normal = Vector3.Up;
            //top left back
            vertices[TLB] = new VertexPositionColorNormal(new Vector3(0, height, -1), color);
            vertices[TLB].Normal = Vector3.Up;
            //top right front
            vertices[TRF] = new VertexPositionColorNormal(new Vector3(1, height, 0), color);
            vertices[TRF].Normal = Vector3.Up;
            //top right back
            vertices[TRB] = new VertexPositionColorNormal(new Vector3(1, height, -1), color);
            vertices[TRB].Normal = Vector3.Up;

            //front Top left
            vertices[FTL] = new VertexPositionColorNormal(new Vector3(0, height, 0), color);
            vertices[FTL].Normal = Vector3.Backward;
            //front Top right
            vertices[FTR] = new VertexPositionColorNormal(new Vector3(1, height, 0), color);
            vertices[FTR].Normal = Vector3.Backward;
            //front Bottom right
            vertices[FBR] = new VertexPositionColorNormal(new Vector3(1, 0, 0), color);
            vertices[FBR].Normal = Vector3.Backward;
            //front Bottom left
            vertices[FBL] = new VertexPositionColorNormal(new Vector3(0, 0, 0), color);
            vertices[FBL].Normal = Vector3.Backward;

            //Left side top front
            vertices[LTF] = new VertexPositionColorNormal(new Vector3(0, height, 0), color);
            vertices[LTF].Normal = Vector3.Left;
            //Left side top back
            vertices[LTB] = new VertexPositionColorNormal(new Vector3(0, height, -1), color);
            vertices[LTB].Normal = Vector3.Left;
            //Left side bottom front
            vertices[LBF] = new VertexPositionColorNormal(new Vector3(0, 0, 0), color);
            vertices[LBF].Normal = Vector3.Left;
            //Left side bottom back
            vertices[LBB] = new VertexPositionColorNormal(new Vector3(0, 0, -1), color);
            vertices[LBB].Normal = Vector3.Left;

            //back top left
            vertices[BTL] = new VertexPositionColorNormal(new Vector3(0, height, -1), color);
            vertices[BTL].Normal = Vector3.Forward;
            //back Top right
            vertices[BTR] = new VertexPositionColorNormal(new Vector3(1, height, -1), color);
            vertices[BTR].Normal = Vector3.Forward;
            //back Bottom right
            vertices[BBR] = new VertexPositionColorNormal(new Vector3(1, 0, -1), color);
            vertices[BBR].Normal = Vector3.Forward;
            //back Bottom left
            vertices[BBL] = new VertexPositionColorNormal(new Vector3(0, 0, -1), color);
            vertices[BBL].Normal = Vector3.Forward;

            //Right side top front
            vertices[RTF] = new VertexPositionColorNormal(new Vector3(1, height, 0), color);
            vertices[RTF].Normal = Vector3.Right;
            //Right side top back
            vertices[RTB] = new VertexPositionColorNormal(new Vector3(1, height, -1), color);
            vertices[RTB].Normal = Vector3.Right;
            //Right side bottom front
            vertices[RBF] = new VertexPositionColorNormal(new Vector3(1, 0, 0), color);
            vertices[RBF].Normal = Vector3.Right;
            //Right side bottom back
            vertices[RBB] = new VertexPositionColorNormal(new Vector3(1, 0, -1), color);
            vertices[RBB].Normal = Vector3.Right;
        }

        private void InitializeIndices()
        {
            indices = new short[36];

            //Front
            indices[0] = FTL; // top left;
            indices[1] = FTR; // top right;
            indices[2] = FBL; // bottom left;

            indices[3] = FBL; // bottom left;
            indices[4] = FTR; // top right;
            indices[5] = FBR; // bottom right;

            //Back
            indices[6] = BBL; // bottom left;
            indices[7] = BTR; // top right;
            indices[8] = BTL; // top left;

            indices[9] = BBR; // bottom right;
            indices[10] = BTR; // top right;
            indices[11] = BBL; // bottom left;

            //Right
            indices[12] = RTB;
            indices[13] = RBF;
            indices[14] = RTF;

            indices[15] = RTB;
            indices[16] = RBB;
            indices[17] = RBF;

            //Left
            indices[18] = LTF;
            indices[19] = LBF;
            indices[20] = LBB;

            indices[21] = LTF;
            indices[22] = LBB;
            indices[23] = LTB;

            //Bottom
            indices[24] = FBL;
            indices[25] = FBR;
            indices[26] = BBL;

            indices[27] = FBR;
            indices[28] = BBR;
            indices[29] = BBL;

            //Top
            indices[30] = TLF;
            indices[31] = TLB;
            indices[32] = TRF;

            indices[33] = TRF;
            indices[34] = TLB;
            indices[35] = TRB;
        }
    }
}
